﻿--[[
   @brief    game_card_tips_control
   @author   hqzheng
   @version  1.0
   @date     2018-07-17
--]]

game_card_tips_control = 
{
    init = function ()
        base_utility.log("[game_card_tips_control] init");
        ierd_tgp.tpf_ui.Require("game_card_tips\\view.lua");
        ierd_tgp.tpf_ui.Require("game_card_tips\\model.lua");
        comm_center.listen_broadcast("i_broadcast_msg_box_new_msg_com", game_card_tips_control.on_broadcast_new_msg);
    end;

    show_tips = function ()
        game_card_tips_view:show(true);
    end;

    on_js_callback = function(uilib, func_name, json_str)
        base_utility.log("[msg_box_frame_control]on_js_callback, func_name:"..func_name..", json_str:"..json_str);
        local param_tbl = base_utility.json_to_table(json_str);
        if param_tbl == nil then
            base_utility.log("[msg_box_frame_control]on_js_callback parse json_str error", true);
            return;
        end

        if func_name == "close" then
            -- 关闭tips
            game_card_tips_view:show(false)
        elseif func_name == "get_cookie" then
            game_card_tips_control.handle_get_cookie(json_str);
        elseif func_name == "get_msg" then
            game_card_tips_control.handle_get_msg(json_str);
        end;
    end;

    -- 获取cookie接口
    handle_get_cookie = function (json_str)
        base_utility.log(string.format("[msg_box_frame_control][handle_get_cookie]json:%s", json_str));
        local param = base_utility.json_to_table(json_str);
        if nil ~= param and nil ~= param.data then
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            rsp_table.data = game_card_tips_model.cookie_info;   
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(param.callback_func, rsp_json, CID_WEB_CTRL_FOR_GAME_CARD_TIPS);
            end
        end
    end;

    handle_get_msg = function (json_str)
        base_utility.log(string.format("[msg_box_frame_control][handle_get_msg]json:%s", json_str));
        local param = base_utility.json_to_table(json_str);
        if nil ~= param and nil ~= param.data then
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            local one_report = {};
            one_report["type"] = game_card_tips_model.MSG_MSGBOX_SYSTEM;
            one_report["app_id"] = game_card_tips_model.MSG_MSGBOX_GAME_CARD_APPID;
            one_report["msg_sub_type"] = game_card_tips_model.MSG_MSGBOX_SUB_TYPE;
            one_report["msg_list"] = game_card_tips_model.msg_list;
            rsp_table.data = one_report;   
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(param.callback_func, rsp_json, CID_WEB_CTRL_FOR_GAME_CARD_TIPS);
            end
        end
    end;

    on_tgp_st_renewed = function(event_id, json_str)
        base_utility.log("[msg_box_frame_control] on_tgp_st_renewed" .. json_str);
        local param_tbl = base_utility.json_to_table(json_str);
        if nil == param_tbl then
            base_utility.log("[msg_box_frame_control][on_tgp_st_renewed] param_tbl is nil");
            return;
        end
        game_card_tips_model.cookie_info["st_key"] = param_tbl["st_key"];
        game_card_tips_model.cookie_info["st"] = param_tbl["st"];
        game_card_tips_model.cookie_info["uin"] = param_tbl["uin"];
        game_card_tips_model.cookie_info["tgp_id"] = param_tbl["tgp_id"];
        game_card_tips_model.cookie_info["tgp_ticket"] = param_tbl["web_token"];

        local callback_func = web_register_center.get_func_by_browser(
            "set_social_cookie", CID_WEB_CTRL_FOR_MESSAGE_BOX);
        if callback_func ~= nil and callback_func ~= "" then
            local rsp_data_table = 
                web_utility.notify_event_comm_data("set_social_cookie");
            rsp_data_table.data.data = game_card_tips_model.cookie_info;
            web_utility.lua_call_js_ex(callback_func, 
                base_utility.table_to_json(rsp_data_table), CID_WEB_CTRL_FOR_GAME_CARD_TIPS);
        end
    end;

    -- 收到服务器通知
    on_broadcast_new_msg = function (data_in)
        if game_card_tips_model.MSG_MSGBOX_GAME_CARD_APPID == data_in.app_id 
            and game_card_tips_model.MSG_MSGBOX_SYSTEM == data_in.msg_type 
            and game_card_tips_model.MSG_MSGBOX_SUB_TYPE == data_in.msg_sub_type then
             -- 清空缓存消息
             game_card_tips_model.msg_list = nil;
             -- 没有游戏在运行，打开窗口
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if nil ~= game_lib and false == game_lib:IsAnyGameRunning() then
                game_card_tips_view:show(true, nil);
            end;
        end
    end;

    handle_banner_switch = function(event_id, json_str)
        ierd_tgp.tpf_ui.Log("[helper_mgr]handle_banner_switch, " .. tostring(json_str));
        local params = base_utility.json_to_table(json_str);
        local game_id = nil;
        if params ~= nil then
            if params.module_id ~= nil then
                game_id = tonumber(params.module_id);
            end
        end
      
        if game_id == nil then
            base_utility.log("[msg_box_frame_control][handle_banner_switch]game_id is nil");
            return;
        end

        if game_id == GAME_ID.GAEM_ID_HOME_PAGE and false == game_card_tips_model.has_try_to_query_msg then        
            -- 查询是否有卡牌消息
            game_card_tips_model.query_game_card_msg(0);
            game_card_tips_model.has_try_to_query_msg = true;
        end;
    end;
}

game_card_tips_control.init();